<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-card">
        <div class="register-header">
          <div class="logo-circle">
            <i class="logo-icon">✨</i>
          </div>
          <h1>创建新账户</h1>
          <p class="subtitle">加入我们，开启内容创作之旅</p>
        </div>

        <form class="register-form" @submit.prevent="handleRegister">
          <div class="form-group">
            <label>
              <i class="icon">👤</i>
              用户名
            </label>
            <input
              v-model="registerForm.username"
              type="text"
              placeholder="请输入用户名（4-20个字符）"
              required
              autocomplete="username"
              minlength="4"
              maxlength="20"
            />
          </div>

          <div class="form-group">
            <label>
              <i class="icon">📧</i>
              邮箱
            </label>
            <input
              v-model="registerForm.email"
              type="email"
              placeholder="请输入邮箱地址"
              required
              autocomplete="email"
            />
          </div>

          <div class="form-group">
            <label>
              <i class="icon">🔒</i>
              密码
            </label>
            <div class="password-input">
              <input
                v-model="registerForm.password"
                :type="showPassword ? 'text' : 'password'"
                placeholder="请输入密码（至少6个字符）"
                required
                autocomplete="new-password"
                minlength="6"
              />
              <button
                type="button"
                class="toggle-password"
                @click="showPassword = !showPassword"
              >
                <i class="icon">{{ showPassword ? '👁️' : '👁️‍🗨️' }}</i>
              </button>
            </div>
          </div>

          <div class="form-group">
            <label>
              <i class="icon">🔐</i>
              确认密码
            </label>
            <div class="password-input">
              <input
                v-model="registerForm.confirmPassword"
                :type="showConfirmPassword ? 'text' : 'password'"
                placeholder="请再次输入密码"
                required
                autocomplete="new-password"
                minlength="6"
              />
              <button
                type="button"
                class="toggle-password"
                @click="showConfirmPassword = !showConfirmPassword"
              >
                <i class="icon">{{ showConfirmPassword ? '👁️' : '👁️‍🗨️' }}</i>
              </button>
            </div>
          </div>

          <div class="form-group">
            <label>
              <i class="icon">🔢</i>
              验证码
            </label>
            <div class="captcha-input">
              <input
                v-model="registerForm.captcha"
                type="text"
                placeholder="请输入验证码"
                required
                maxlength="6"
                autocomplete="off"
              />
              <div class="captcha-image" @click="refreshCaptcha">
                <img 
                  v-if="captchaImageUrl" 
                  :src="captchaImageUrl" 
                  alt="验证码"
                  title="点击刷新"
                />
                <div v-else class="captcha-loading">
                  <i class="icon">⏳</i>
                </div>
              </div>
            </div>
          </div>

          <div class="form-group checkbox-group">
            <label class="checkbox-label">
              <input type="checkbox" v-model="registerForm.agreeTerms" required />
              <span>我已阅读并同意<a href="#" class="link">服务条款</a>和<a href="#" class="link">隐私政策</a></span>
            </label>
          </div>

          <button type="submit" class="btn-register" :disabled="isLoading">
            <span v-if="!isLoading">
              <i class="icon">🚀</i>
              立即注册
            </span>
            <span v-else>
              <i class="icon loading">⏳</i>
              注册中...
            </span>
          </button>
        </form>

        <div class="register-footer">
          <p>已有账户？<router-link to="/login" class="login-link">立即登录</router-link></p>
        </div>

        <div class="social-register">
          <div class="divider">
            <span>或使用以下方式注册</span>
          </div>
          <div class="social-buttons">
            <button class="social-btn" @click="handleSocialRegister('wechat')">
              <i class="icon">💬</i>
              微信
            </button>
            <button class="social-btn" @click="handleSocialRegister('qq')">
              <i class="icon">🐧</i>
              QQ
            </button>
            <button class="social-btn" @click="handleSocialRegister('github')">
              <i class="icon">🐙</i>
              GitHub
            </button>
          </div>
        </div>
      </div>

      <div class="decoration-circles">
        <div class="circle circle-1"></div>
        <div class="circle circle-2"></div>
        <div class="circle circle-3"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { register, getCaptcha } from '../api/auth'
import { API_BASE_URL } from '../api/config'

const router = useRouter()

const registerForm = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  captcha: '',
  captchaKey: '',
  agreeTerms: false
})

const showPassword = ref(false)
const showConfirmPassword = ref(false)
const isLoading = ref(false)
const captchaImageUrl = ref('')

// 获取验证码
const loadCaptcha = async () => {
  try {
    const result = await getCaptcha()
    if (result.code === 200 && result.data) {
      registerForm.value.captchaKey = result.data.captchaKey
      captchaImageUrl.value = `${API_BASE_URL}${result.data.captchaImage}`
    }
  } catch (error) {
    console.error('获取验证码失败:', error)
    alert('❌ 获取验证码失败，请刷新页面重试')
  }
}

// 刷新验证码
const refreshCaptcha = () => {
  registerForm.value.captcha = ''
  loadCaptcha()
}

// 页面加载时获取验证码
onMounted(() => {
  loadCaptcha()
})

// 表单验证
const validateForm = () => {
  // 用户名验证
  if (registerForm.value.username.trim().length < 4) {
    alert('❌ 用户名至少需要4个字符')
    return false
  }

  // 邮箱验证
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  if (!emailRegex.test(registerForm.value.email)) {
    alert('❌ 请输入有效的邮箱地址')
    return false
  }

  // 密码验证
  if (registerForm.value.password.length < 6) {
    alert('❌ 密码至少需要6个字符')
    return false
  }

  // 确认密码验证
  if (registerForm.value.password !== registerForm.value.confirmPassword) {
    alert('❌ 两次输入的密码不一致')
    return false
  }

  // 验证码验证
  if (!registerForm.value.captcha.trim()) {
    alert('❌ 请输入验证码')
    return false
  }

  // 服务条款验证
  if (!registerForm.value.agreeTerms) {
    alert('❌ 请先阅读并同意服务条款和隐私政策')
    return false
  }

  return true
}

const handleRegister = async () => {
  // 表单验证
  if (!validateForm()) {
    return
  }

  // 开始注册
  isLoading.value = true

  try {
    // 调用注册接口，包含验证码参数
    const result = await register({
      username: registerForm.value.username,
      email: registerForm.value.email,
      password: registerForm.value.password,
      confirmPassword: registerForm.value.confirmPassword,
      captcha: registerForm.value.captcha,
      captchaKey: registerForm.value.captchaKey
    })

    // 注册成功
    alert('✅ 注册成功！即将跳转到登录页面')
    
    // 跳转到登录页
    setTimeout(() => {
      router.push('/login')
    }, 1000)
  } catch (error) {
    // 注册失败，刷新验证码
    console.error('注册错误:', error)
    alert('❌ 注册失败：' + (error.message || '请稍后重试'))
    refreshCaptcha()
  } finally {
    isLoading.value = false
  }
}

const handleSocialRegister = (platform) => {
  alert(`🚧 ${platform} 注册功能即将上线`)
}
</script>

<style scoped>
.register-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  position: relative;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.register-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
}

.register-card {
  background: white;
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: slideUp 0.5s ease;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-circle {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  box-shadow: 0 10px 30px rgba(102, 126, 234, 0.3);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.logo-icon {
  font-size: 40px;
  font-style: normal;
}

.register-header h1 {
  font-size: 28px;
  color: #2c3e50;
  margin-bottom: 10px;
  font-weight: 700;
}

.subtitle {
  color: #7f8c8d;
  font-size: 15px;
  margin: 0;
}

.icon {
  font-style: normal;
}

.register-form {
  margin-bottom: 25px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #555;
  margin-bottom: 8px;
  font-size: 14px;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  font-size: 15px;
  transition: all 0.3s;
  font-family: inherit;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}

.password-input {
  position: relative;
}

.password-input input {
  padding-right: 45px;
}

.toggle-password {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  font-size: 18px;
  color: #7f8c8d;
  transition: all 0.3s;
  padding: 5px;
}

.toggle-password:hover {
  color: #667eea;
}

/* 验证码输入框样式 */
.captcha-input {
  display: flex;
  gap: 12px;
  align-items: center;
}

.captcha-input input {
  flex: 1;
}

.captcha-image {
  width: 120px;
  height: 46px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.3s;
  background: #f8f9fa;
  display: flex;
  align-items: center;
  justify-content: center;
}

.captcha-image:hover {
  border-color: #667eea;
  transform: scale(1.02);
}

.captcha-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.captcha-loading {
  font-size: 24px;
  animation: rotate 1s linear infinite;
}

.checkbox-group {
  margin-bottom: 25px;
}

.checkbox-label {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  color: #555;
  font-size: 14px;
  font-weight: normal;
}

.checkbox-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: #667eea;
  flex-shrink: 0;
  margin-top: 2px;
}

.checkbox-label .link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
}

.checkbox-label .link:hover {
  text-decoration: underline;
}

.btn-register {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.btn-register:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 25px rgba(102, 126, 234, 0.4);
}

.btn-register:active:not(:disabled) {
  transform: translateY(0);
}

.btn-register:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.loading {
  animation: rotate 1s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.register-footer {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #e0e0e0;
  color: #7f8c8d;
  font-size: 14px;
}

.login-link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  margin-left: 5px;
  transition: all 0.3s;
}

.login-link:hover {
  color: #764ba2;
  text-decoration: underline;
}

.social-register {
  margin-top: 25px;
}

.divider {
  text-align: center;
  margin-bottom: 20px;
  position: relative;
}

.divider::before,
.divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 40%;
  height: 1px;
  background: #e0e0e0;
}

.divider::before {
  left: 0;
}

.divider::after {
  right: 0;
}

.divider span {
  background: white;
  padding: 0 15px;
  color: #7f8c8d;
  font-size: 13px;
  position: relative;
}

.social-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.social-btn {
  flex: 1;
  padding: 10px;
  background: white;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: #555;
  font-weight: 600;
}

.social-btn:hover {
  border-color: #667eea;
  background: #f8f9ff;
  transform: translateY(-2px);
}

.social-btn .icon {
  font-size: 24px;
}

/* 装饰圆圈 */
.decoration-circles {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  pointer-events: none;
  overflow: hidden;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  animation: float 20s ease-in-out infinite;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -150px;
  left: -150px;
  animation-delay: 0s;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -100px;
  right: -100px;
  animation-delay: 5s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  top: 50%;
  right: -75px;
  animation-delay: 10s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-page {
    padding: 15px;
    padding-top: 20px;
    padding-bottom: 20px;
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
    height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }

  .register-container {
    max-width: 100%;
    margin: 0 auto;
  }

  .register-card {
    padding: 25px 20px;
    border-radius: 15px;
  }

  .register-header {
    margin-bottom: 25px;
  }

  .register-header h1 {
    font-size: 22px;
  }

  .subtitle {
    font-size: 14px;
  }

  .logo-circle {
    width: 65px;
    height: 65px;
    margin-bottom: 15px;
  }

  .logo-icon {
    font-size: 32px;
  }

  /* 表单组优化 */
  .form-group {
    margin-bottom: 18px;
  }

  .form-group label {
    font-size: 13px;
    gap: 6px;
  }

  .form-group input {
    padding: 11px 14px;
    font-size: 14px;
    border-radius: 8px;
  }

  /* 密码输入框 */
  .password-input input {
    padding-right: 40px;
  }

  .toggle-password {
    right: 10px;
    font-size: 16px;
  }

  /* 验证码区域优化 */
  .captcha-input {
    gap: 10px;
  }

  .captcha-image {
    width: 100px;
    height: 44px;
    flex-shrink: 0;
  }

  /* 按钮优化 */
  .btn-register {
    padding: 13px;
    font-size: 15px;
    border-radius: 8px;
  }

  /* 社交按钮 */
  .social-buttons {
    gap: 8px;
  }

  .social-btn {
    padding: 12px 8px;
    font-size: 11px;
    border-radius: 8px;
  }

  .social-btn .icon {
    font-size: 20px;
  }

  /* 复选框标签 */
  .checkbox-label {
    font-size: 13px;
    line-height: 1.4;
  }

  .checkbox-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
  }

  /* 页脚 */
  .register-footer {
    font-size: 13px;
    padding-top: 18px;
  }

  /* 分割线 */
  .divider span {
    font-size: 12px;
    padding: 0 10px;
  }

  .social-register {
    margin-top: 20px;
  }
}

@media (max-width: 480px) {
  .register-page {
    padding: 10px;
    padding-top: 15px;
    padding-bottom: 15px;
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
  }

  .register-container {
    margin: 0 auto;
  }

  .register-card {
    padding: 20px 16px;
    border-radius: 12px;
    margin-bottom: 10px;
  }

  .register-header {
    margin-bottom: 20px;
  }

  .register-header h1 {
    font-size: 20px;
  }

  .subtitle {
    font-size: 13px;
  }

  .logo-circle {
    width: 55px;
    height: 55px;
    margin-bottom: 12px;
  }

  .logo-icon {
    font-size: 26px;
  }

  /* 表单组进一步优化 */
  .form-group {
    margin-bottom: 14px;
  }

  .form-group label {
    font-size: 12px;
    margin-bottom: 6px;
  }

  .form-group input {
    padding: 10px 12px;
    font-size: 14px;
  }

  /* 验证码保持横向，但缩小尺寸 */
  .captcha-input {
    gap: 8px;
  }

  .captcha-image {
    width: 90px;
    height: 40px;
  }

  /* 社交按钮堆叠显示 */
  .social-buttons {
    flex-direction: column;
    gap: 8px;
  }

  .social-btn {
    flex-direction: row;
    justify-content: center;
    gap: 8px;
    padding: 10px;
    font-size: 13px;
  }

  .social-btn .icon {
    font-size: 20px;
  }

  /* 按钮优化 */
  .btn-register {
    padding: 11px;
    font-size: 14px;
  }

  /* 复选框区域 */
  .checkbox-group {
    margin-bottom: 16px;
  }

  .checkbox-label {
    gap: 8px;
  }

  /* 页脚优化 */
  .register-footer {
    padding-top: 15px;
    margin-bottom: 0;
  }

  /* 社交注册区域 */
  .social-register {
    margin-top: 18px;
  }

  .divider {
    margin-bottom: 15px;
  }

  /* 装饰圆圈在小屏幕上缩小 */
  .circle-1 {
    width: 200px;
    height: 200px;
  }

  .circle-2 {
    width: 150px;
    height: 150px;
  }

  .circle-3 {
    width: 100px;
    height: 100px;
  }
}

/* 超小屏幕优化（如 iPhone SE） */
@media (max-width: 375px) {
  .register-card {
    padding: 18px 14px;
  }

  .register-header h1 {
    font-size: 19px;
  }

  .form-group input {
    font-size: 13px;
  }

  .btn-register {
    font-size: 14px;
    padding: 11px;
  }
}

/* 横屏模式优化 */
@media (max-width: 768px) and (orientation: landscape) {
  .register-page {
    padding-top: 10px;
    padding-bottom: 10px;
  }

  .register-card {
    padding: 20px;
  }

  .logo-circle {
    width: 50px;
    height: 50px;
    margin-bottom: 10px;
  }

  .logo-icon {
    font-size: 24px;
  }

  .register-header {
    margin-bottom: 20px;
  }

  .register-header h1 {
    font-size: 20px;
    margin-bottom: 5px;
  }

  .subtitle {
    font-size: 12px;
  }

  .form-group {
    margin-bottom: 12px;
  }

  .social-register {
    margin-top: 15px;
  }

  .divider {
    margin-bottom: 12px;
  }
}
</style>
